package com.example.aidl;

import java.lang.reflect.Method;

import android.app.Activity;
import android.app.ActivityManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.os.RemoteException;
import android.widget.Button;

import com.googlecode.androidannotations.annotations.Click;
import com.googlecode.androidannotations.annotations.EActivity;
import com.googlecode.androidannotations.annotations.ViewById;

@EActivity(R.layout.activity_main)
public class MainActivity extends Activity {
	PeopleAIDL peopleAIDL;
	@ViewById
	Button button1,button2,button3,button4,button5;

	ServiceConnection conn = new ServiceConnection(){
		public void onServiceConnected(ComponentName className, IBinder service) {
			//服务绑定成功，绑定动作好像是异步的
			peopleAIDL=PeopleAIDL.Stub.asInterface(service);
		}

		public void onServiceDisconnected(ComponentName className) {
		
		}
	};
	//下面是几个按钮的点击事件
	@Click
	void button1()
	{
		//绑定启动服务
		bindService(new Intent(this, AIDLService_.class), conn, Context.BIND_AUTO_CREATE);
		startService(new Intent(this, AIDLService_.class));
		

		
	}
	@Click
	void button2()
	{
		try {
			peopleAIDL.setName("zhangshan");
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	@Click
	void button3()
	{
		try {
			peopleAIDL.setAge(23);
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	@Click
	void button4()
	{
		try {
			peopleAIDL.say();
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	@Click
	void button5()
	{
		//SecondActivity后加_是因为用了androidannotations
		Intent intent=new Intent(this,SecondActivity_.class);
		startActivity(intent);
	}
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		//退出时必须unbindService
		unbindService(conn);
		//如果Service仍需要在后台运行，不需要下面的这句
		stopService(new Intent(this, AIDLService_.class));
	}
}
